<template>
	<view v-if="show" class="my-screenshot">
		<ksp-cropper
			mode="fixed"
			:width="200"
			:height="200"
			:maxWidth="1024"
			:maxHeight="1024"
			:url="path"
			@cancel="onCancel"
			@ok="onOk"
		/>
	</view>
</template>

<script>
	import { imgToBase64, base64ToFile } from '@/utils';
	import { uploadFileByPath } from '@/api/upload';
	
	export default {
		props: {
			show: {
				type: Boolean,
				default: false,
			},
			path: {
				type: String,
				default: '',
			},
		},
		methods: {
			select() {
				uni.chooseImage({
					count: 1,
					success: (rst) => {
						// 设置url的值，显示控件
						this.image = rst.tempFilePaths[0];
					}
				});
			},
			onOk(ev) {
				uploadFileByPath(ev.path).then(res => {
					if (res.code === 200) {
						this.$emit('ok', res.data.urlCdn);
						this.onCancel();
					}
				});
			},
			onCancel() {
				// url设置为空，隐藏控件
				this.image = "";
				this.$emit('cancel');
			}
		}
	}
</script>
<style lang="scss" scoped>
	.my-screenshot {
		position: fixed;
		top: 0;
		bottom: 0;
		left: 0;
		right: 0;
		margin: auto;
		background-color: #000;
		z-index: 100;
	}
</style>